package cn.cl.gulimall.coupon.service.impl;

import cn.cl.common.to.MemberPrice;
import cn.cl.common.to.SkuReductionTo;
import cn.cl.gulimall.coupon.entity.MemberPriceEntity;
import cn.cl.gulimall.coupon.entity.SkuLadderEntity;
import cn.cl.gulimall.coupon.service.MemberPriceService;
import cn.cl.gulimall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.cl.common.utils.PageUtils;
import cn.cl.common.utils.Query;

import cn.cl.gulimall.coupon.dao.SkuFullReductionDao;
import cn.cl.gulimall.coupon.entity.SkuFullReductionEntity;
import cn.cl.gulimall.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		IPage<SkuFullReductionEntity> page = this.page(
				new Query<SkuFullReductionEntity>().getPage(params),
				new QueryWrapper<SkuFullReductionEntity>()
		);

		return new PageUtils(page);
	}

	@Autowired
	private SkuLadderService skuLadderService;
	@Autowired
	private MemberPriceService memberPriceService;

	/**
	 * 6.4、sku的优惠、满减等信息；gulimall_sms->sms_sku_ladder\sms_sku_full_reduction\sms_member_price
	 */
	@Override
	public void saveSkuReduction(SkuReductionTo skuReductionTo) {
		// 1、gulimall_sms->sms_sku_ladder
		SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
		skuLadderEntity.setSkuId(skuReductionTo.getSkuId());
		skuLadderEntity.setFullCount(skuReductionTo.getFullCount());
		skuLadderEntity.setDiscount(skuReductionTo.getDiscount());
		skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());
		if (skuReductionTo.getFullCount() > 0) {
			skuLadderService.save(skuLadderEntity);
		}
		// 2、sms_sku_full_reduction
		SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
		BeanUtils.copyProperties(skuReductionTo, skuFullReductionEntity);
		if (skuReductionTo.getFullPrice().compareTo(new BigDecimal(0)) == 1) {
			this.save(skuFullReductionEntity);
		}
		// 3、sms_member_price
		List<MemberPrice> memberPrices = skuReductionTo.getMemberPrices();
		if (memberPrices == null) {
			return;
		}
		List<MemberPriceEntity> memberPriceEntities = memberPrices.stream().map((memberPrice) -> {
			MemberPriceEntity priceEntity = new MemberPriceEntity();
			priceEntity.setSkuId(skuReductionTo.getSkuId());
			priceEntity.setMemberLevelId(memberPrice.getId());
			priceEntity.setMemberLevelName(memberPrice.getName());
			priceEntity.setMemberPrice(memberPrice.getPrice());
			priceEntity.setAddOther(1);
			return priceEntity;
		}).filter((memberPrice) -> {
			return memberPrice.getMemberPrice().compareTo(new BigDecimal(0)) == 1;
		}).collect(Collectors.toList());
		memberPriceService.saveBatch(memberPriceEntities);
	}
}